package com.etc.logManage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.common.exception.DataOperationException;
import com.etc.logManage.dao.ILogLoginMapper;
import com.etc.logManage.model.LogLoginModel;
import com.etc.logManage.service.ILogLoginService;
import com.etc.logManage.vo.LogLoginVO;
import com.github.pagehelper.PageHelper;

/**
 * @Title: LogLoginServiceImpl.java
 * @Package com.etc.logManage.service.impl
 * @Description: TODO
 * @author 黄宇
 * @date 2016年3月27日 下午10:33:48
 * @version V1.0
 */
@Service("logLoginService")
public class LogLoginServiceImpl implements ILogLoginService {

	@Autowired
	private ILogLoginMapper logLoginMapper;

	@Override
	public Integer insert(LogLoginVO vo) {
		try {
			LogLoginModel model = vo.vo2Model();
			return this.logLoginMapper.insert(model);
		} catch (Exception e) {
			String errMsg = String.format("登录日志保存异常,参数 :vo=%s", vo);
			throw new DataOperationException(errMsg, e);
		}
	}

	@Override
	public void delete(Integer id) {
		try {
			this.logLoginMapper.delete(id);
		} catch (Exception e) {
			String errMsg = String.format("登录日志删除异常,参数 :id=%s", id);
			throw new DataOperationException(errMsg, e);
		}
	}

	@Override
	public void update(LogLoginVO vo) {
		try {
			LogLoginModel model = vo.vo2Model();
			this.logLoginMapper.update(model);
		} catch (Exception e) {
			String errMsg = String.format("登录日志修改异常,参数 :vo=%s", vo);
			throw new DataOperationException(errMsg, e);
		}
	}

	@Override
	public LogLoginVO selectById(Integer id) {
		try {
			LogLoginModel model = this.logLoginMapper.selectById(id);
			LogLoginVO vo = model.model2VO();
			return vo;
		} catch (Exception e) {
			String errMsg = String.format("登录日志修改异常,参数 :id=%s", id);
			throw new DataOperationException(errMsg, e);
		}
	}

	@Override
	public List<LogLoginVO> list(Map<String, Object> param) {
		try {
			PageHelper.startPage((Integer) param.get("pageNo"), (Integer) param.get("pageNum"));
			List<LogLoginModel> modelList = this.logLoginMapper.list(param);
			List<LogLoginVO> voList = new ArrayList<LogLoginVO>();

			for (LogLoginModel model : modelList) {
				LogLoginVO vo = model.model2VO();
				voList.add(vo);
			}
		} catch (Exception e) {
			String errMsg = String.format("登录日志查询异常,参数 :param=%s", param);
			throw new DataOperationException(errMsg, e);
		}
		return null;
	}

	@Override
	public int count(Map<String, Object> param) {
		try {
			return this.logLoginMapper.count(param);
		} catch (Exception e) {
			String errMsg = String.format("登录日志查询异常,参数 :param=%s", param);
			throw new DataOperationException(errMsg, e);
		}
	}
}
